var express = require("express");
var router = express.Router();
const Result = require("../../utils/result");
const activityService = require("../services/activity");

//添加活动
router.post("/activity", async function (req, res) {
  const { activity } = req.body;
  await activityService.addActivity(activity);
  res.json(Result.success());
});

//获取一条活动
router.get("/activity/:id", async function (req, res) {
  const id = req.params.id;
  const activity = await activityService.getActivity(id);
  res.json(Result.success({ activity }));
});

//更新一条活动
router.put("/activity/:id", async function (req, res) {
  const id = req.params.id;
  const activity = req.body.activity;
  await activityService.updateActivity(id, activity);
  res.json(Result.success());
});

//删除一条活动
router.delete("/activity/:id", async function (req, res) {
  const id = req.params.id;
  await activityService.deleteActivity(id);
  res.json(Result.success());
});

//获取活动列表
router.get("/getList", async function (req, res) {
  const { keyword, page, size } = req.query;
  const { rows, count } = await activityService.getActivityList(
    keyword,
    page,
    size
  );
  res.json(Result.success({ activitys: rows, count }));
});

module.exports = router;
